﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Xrm.Sdk.Messages;
using Xrm.Framework.Test.Integration;
using Xrm.Solutions.FrameworkSample.Common.Entities;


namespace Xrm.Solutions.FrameworkSample.XrmIntegrationTests.WFActivities
{
    [TestClass]
    public class TestTotalOpportunities : WFActivityIntegrationTest
    {
        #region Instance Variables

        private Account account;

        #endregion

        #region Properties

        protected override string AsyncOperationName
        {
            get
            {
                return "Test Total Opportunities WF";
            }
        }

        #endregion

        #region Setup

        protected override Guid SetupPrimaryEntity()
        {
            CrmHelpers.SetConfigurationSetting(this.OrganizationService, "AutoNumberPrefix", "Test" + DateTime.Now);
            
            account = new Account();
            account.Name = "Test Total Opportunities WF " + DateTime.Now;
            account.Id = this.OrganizationService.Create(account);

            return account.Id;
        }

        #endregion

        #region Test

        [TestMethod]
        public void RunTestTotalOpportunities()
        {
            base.Test();
        }

        #endregion

        #region Verify

        protected override void VerifyStatus()
        {
            Assert.AreEqual(Status, AsyncStatus.Pass);
        }

        #endregion

        #region Clean up

        protected override void CleanUp()
        {
            base.CleanUp();

            using (XrmServiceContext xrmContext = new XrmServiceContext(this.OrganizationService))
            {
                var opps = from opp in xrmContext.OpportunitySet
                           where opp.AccountId.Id == account.Id
                           select opp.Id;

                foreach (Guid oppId in opps)
                {
                    this.OrganizationService.Delete("opportunity", oppId);
                }
            }

            this.OrganizationService.Delete(account.LogicalName, account.Id);

        }

        #endregion
    }
}
